<template>
    <el-container class="home">
      <!-- 头部 -->
      <el-header class="home-header">
        <el-row type="flex" justify="space-between">
          <el-col :span="4">
            <div class="grid-content bg-purple">
              <img src="@/assets/images/logo.png" alt="黑马程序员">
            </div>
          </el-col>
          <el-col :span="12">
            <h3>电商后台管理系统</h3>
          </el-col>
          <el-col :span="4" class="logout-desc">
            欢迎上海前端28期星曜会员
            <a href="javascript:;" class="logout" @click.prevent="logout">退出</a>
          </el-col>
        </el-row>
      </el-header>

      <!-- 下边： -->
      <el-container class="home-content">
        <!-- 左侧侧边栏 -->
        <el-aside width="200px" class="home-content-aside">
          <el-menu
             :unique-opened="true"
             :router="true"
             default-active="$route.path.split('/').length === 3 ? ('/' + $route.path.split('/')[1]): $route.path"
             class="el-menu-vertical-demo"
             background-color="#545c64"
             text-color="#fff"
             active-text-color="#ffd04b">
               <!-- 一级菜单 -->
        <el-submenu :index=" level1.id + '' " v-for="level1 in menus" :key="level1.id">
               <!-- template 用来给该菜单添加菜单名称和 -->
               <template slot="title">
                 <!-- 菜单图标 -->
                 <i class="el-icon-location"></i>
                 <!-- 菜单名称 -->
                 <span>{{ level1.authName }}</span>
              </template>
             <!-- 二级菜单 -->
             <el-menu-item :index="'/'+level2.path" v-for="level2 in level1.children" :key="level2.id">
               <!-- 菜单的图标 -->
               <i class="el-icon-menu"></i>
               <!-- 菜单的名称 -->
               <span slot="title">{{ level2.authName }}</span>
             </el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>

        <!-- 右侧内容区域 -->
        <el-main>
          <!-- 子路由出口 -->
          <router-view></router-view>
        </el-main>
        </el-container>
        </el-container>
</template>

<script>
export default {
  created () {
    this.getMenus()
  },

  data () {
    return {
      // 菜单数据
      menus: []
    }
  },
  methods: {
    // 退出
    logout () {
      this.$confirm('是否确定退出?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '退出成功!'
        })
        // 退出后，要跳转到登陆页面
        this.$router.push('/login')
        // 3移除token
        localStorage.removeItem('token')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消退出'
        })
      })
    },

    // 动态获取菜单数据
    async getMenus () {
      const res = await this.$http.get('/menus')
      this.menus = res.data.data
    }
  }
}
</script>

<style>
.home {
  height: 100%;
}

.home-header {
  padding-left: 0;
  background-color: #b3c1cd;
}

.home-header img {
  width: 200px;
}

.home-header h3 {
  height: 60px;
  line-height: 60px;
  text-align: center;
  font-size: 28px;
  color: #fff;
}

.home-header .logout-desc {
  min-width: 236px;
  line-height: 60px;
  font-weight: bold;
}

.home-header .logout-desc .logout {
  color: #daa520;
}

.home-content {
  background-color: #eaeef1;
}

.home-content-aside {
  background-color: #545c64;
}
</style>
